//index.js
//获取应用实例
const app = getApp()

Page({
    data: {
        motto: 'Hello World',
        cartData: [],
		productBaseUrl: '',
		focus: false,
		inputValue: false,
        totalPrice: 0
    },
    //事件处理函数
    bindViewTap: function() {
        wx.navigateTo({
          url: '../logs/logs'
        })
    },
	bindfocus: function () {

	},
	bindKeyInput: function(e) {

		console.log('keyInput', e.detail.value)
		let curProduct = e.currentTarget.dataset.productInfo;
		console.log('bindKeyInput',curProduct)
		var that = this;
		wx.getStorage({
			key: 'cartList',
			success: function(res) {
				let cartObj = res.data
				console.log('getstorage', cartObj)
				console.log('getstorage', curProduct)
				if(cartObj && cartObj[curProduct.id]){
					if( e.detail.value == 0) {
						cartObj[curProduct.id] = null
					} else {
						cartObj[curProduct.id].num = Number(e.detail.value);
                    }
				}
				app.globalData.cartList = {...cartObj};
                let cartObjData = {};
				for(let key in cartObj) {
					if(cartObj[key]) {
						cartObjData[key] = cartObj[key]
					}

				}
				var arr = Object.values(cartObjData)
				that.setData({
					cartData : [...arr]
				})
                if(arr.length > 0) {
					wx.setStorage({
						key:"cartList",
						data: cartObjData
					})
                } else {
					wx.removeStorage({
						key: 'cartList',
						success: function(res) {
							console.log('removeStorage',res.data)
						}
					})
                }
			},
			fail: function (res) {
				console.log('fail', res)
			}
		});
	},
	addProduct: function (e) {
		let curProduct = e.currentTarget.dataset.productInfo;
		var that = this;
		let cartObj = {};
		wx.getStorage({
			key: 'cartList',
			success: function(res) {
				cartObj = res.data
                console.log('add click',cartObj)
				if(cartObj && cartObj[curProduct.id]){
					cartObj[curProduct.id].num ++ ;
				}
				app.globalData.cartList = {...cartObj};
				var arr = Object.values(cartObj)
				var totalPriceData = 0;
				arr.forEach(function (item) {
					totalPriceData += (item.price *item.num);
				})
				that.setData({
					cartData : [...arr],
					totalPrice: totalPriceData
				})
				wx.setStorage({
					key:"cartList",
					data: cartObj
				})
			},
			fail: function (res) {
				console.log('fail', res)
			}
		});
	},
	reduceProduct: function (e) {
		let curProduct = e.currentTarget.dataset.productInfo;
		let cartObj = {};
		var that = this;
		wx.getStorage({
			key: 'cartList',
			success: function(res) {
				cartObj = res.data
				if(cartObj[curProduct.id].num > 1){
					cartObj[curProduct.id].num -- ;
				} else {
					cartObj[curProduct.id] = null
                }
                let cartObjData = {};
				for(let key in cartObj) {
					if(cartObj[key]) {
						cartObjData[key] = cartObj[key]
					}

				}
				app.globalData.cartList = {...cartObjData};
				var arr = Object.values(cartObjData)
				var totalPriceData = 0;
				arr.forEach(function (item) {
					totalPriceData += (item.price *item.num);
				})
				that.setData({
					cartData : [...arr],
					totalPrice: totalPriceData
				})
				if(arr.length > 0) {
					wx.setStorage({
						key:"cartList",
						data: cartObjData
					})
				} else {
					wx.removeStorage({
						key: 'cartList',
						success: function(res) {
							console.log('removeStorage',res.data)
						}
					})
				}
			},
			fail: function (res) {
				console.log('fail', res)
			}
		});
	},
    checkout: function () {
		var that = this;
		wx.getStorage({
			key: 'cartList',
			success: function(res) {
				var productList = res.data,
                    payTotalPrice = 0,
                    paymentListData = {};
				Object.values(productList).forEach(function (item) {
					payTotalPrice += item.price * item.num
				});
				paymentListData = {
					productList,
					payTotalPrice
                }
				wx.setStorage({
					key:"paymentList",
					data: paymentListData
				})
				wx.removeStorage({
					key: 'cartList',
					success: function(res) {
						that.setData({
							cartData : []
						})
						console.log('removeStorage',res.data)
					}
				})
				wx.navigateTo({
					url: '../payment/index'
				})
			},
			fail: function (res) {
				console.log('fail checkout', res)
				that.setData({
					cartData : []
				})
			}
		});
	},
    onLoad: function () {
		wx.getStorage({
			key: 'cartList',
			success: function(res) {
				console.log('get onlode', res.data)

			}
		})

    },
	onHide: function() {
		console.log('onHide')
    },

	onLaunch: function () {
        console.log('onlaunch')

	},
	onShow: function() {
		this.setData({
			productBaseUrl: app.globalData.productBaseUrl
		});
		var that = this;
		wx.getStorage({
			key: 'cartList',
			success: function(res) {
				var arr = Object.values(res.data)
				var totalPriceData = 0;
				arr.forEach(function (item) {
					totalPriceData += (item.price *item.num);
				})
				that.setData({
					cartData : [...arr],
					totalPrice: totalPriceData
				})
			}
		})

		console.log('onShow', this.data)
		console.log('onShow', Object.values(this.data.cartData))
		wx.setNavigationBarTitle({
			title: 'Cart'
		});
    },

	onUnlaunch: function() {
		console.log('onUnlaunch')
    },

	userInfoReadyCallback: function() {
		console.log('userInfoReadyCallback')
    },

})
